package com.galaxy.service.controller.system;

import com.galaxy.commom.base.annotation.JwtClaim;
import com.galaxy.commom.base.annotation.WebLog;
import com.galaxy.commom.base.jwt.JwtToken;
import com.galaxy.commom.base.message.ResponseCode;
import com.galaxy.commom.base.message.ResponseResult;
import com.galaxy.service.dto.SignInDto;
import com.galaxy.service.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账号控制类
 * @author caijinbang
 * @date 2018/10/28 15:03
 */
@RestController
@RequestMapping(value = {"/account"})
@Api(tags = {"账户相关"})
public class AccountController {

  @Autowired
  private UserService userService;

  @PostMapping(value = {"/sign-in"})
  @ApiOperation(value = "登录")
  @WebLog("登录")
  public ResponseResult signIn(@RequestBody @Validated @ApiParam(value = "登录数据",required = true)
      SignInDto signInDto){
    userService.signIn(signInDto);
    return ResponseResult.e(ResponseCode.SIGN_IN_OK,((JwtToken) SecurityUtils.getSubject().getPrincipal()).getToken());
  }

  @PostMapping(value = "/current")
  @ApiOperation(value = "获取当前用户信息")
  @WebLog("获取当前用户信息")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult current(){
    return ResponseResult.e(ResponseCode.OK, userService.getCurrentUser());
  }

  @PostMapping(value = "/logout")
  @ApiOperation(value = "注销登录")
  @WebLog("注销登录")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult logout(){
    try {
      Subject subject = SecurityUtils.getSubject();
      subject.logout();
    }catch (Exception e){
      return ResponseResult.e(ResponseCode.LOGOUT_FAIL);
    }
    return ResponseResult.e(ResponseCode.LOGOUT_OK);
  }

  @PostMapping(value = "/all-permission-tag")
  @ApiOperation(value = "获取所有的权限标示")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult<List<String>> getAllPermissionTag(@JwtClaim String t){
    return ResponseResult.e(ResponseCode.OK,userService.getAllPermissionTag());
  }
}
